package com.cct.digitalarchapi.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cct.digitalarchapi.common.Response;
import com.cct.digitalarchapi.dao.ArchApplicationMapper;
import com.cct.digitalarchapi.dao.ArchCategoryMapper;
import com.cct.digitalarchapi.dao.DigitalArchMainMapper;
import com.cct.digitalarchapi.pojo.ArchApplication;
import com.cct.digitalarchapi.pojo.dto.ArchMainDto;
import com.cct.digitalarchapi.pojo.vo.*;
import com.cct.digitalarchapi.service.ArchApplicationService;
import com.cct.digitalarchapi.service.ChartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @FileName ArchCategoryServiceImpl
 * @Description 实现类
 * @Author Created by chenmingzhe
 * @Date 2023/11/19
 **/
@Slf4j
@Service
public class ChartServiceImpl implements ChartService {

    @Autowired
    private DigitalArchMainMapper digitalArchMainMapper;
    @Autowired
    private ArchCategoryMapper archCategoryMapper;

    @Override
    public Response getTodaySubmit() {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
            String time = simpleDateFormat.format(new Date());
            String year=time.substring(0,4);
            List<ArchMainCountVo> list = digitalArchMainMapper.getCateCountToday(time);
            List<ArchMainCountVo> categoryList = archCategoryMapper.getCategory();
            List<String> xdata = new ArrayList<>();
            List<ArchMainDto> seriesData = new ArrayList<>();
            for (ArchMainCountVo archMainCountVo : categoryList) {
                xdata.add(archMainCountVo.getArchCateName());
                ArchMainDto archMainDto = new ArchMainDto();
                archMainDto.setArchCateName(archMainCountVo.getArchCateName());
                archMainDto.setCount(0);
                for (ArchMainCountVo mainCountVo : list) {
                    if (mainCountVo.getArchCateCode().equals(archMainCountVo.getArchCateCode())) {
                        archMainDto.setCount(mainCountVo.getCount());
                    }
                }
                seriesData.add(archMainDto);
            }
            int submitCountToday = digitalArchMainMapper.getSubmitCountToday(time);
            int allPassCount = digitalArchMainMapper.getAllCountByState(5);
            int allNoPassCount = digitalArchMainMapper.getAllCountByState(3);
            int isAuditCount = digitalArchMainMapper.getAllCountByState(1);
            int allPassCountToday=digitalArchMainMapper.getPassCountToday(time,5);
            int allPassCountYear=digitalArchMainMapper.getAllPassCountYear(year);
            int allSubmitCountYear=digitalArchMainMapper.getAllSubmitCountYear(year);
            ArchMainSubmitVo archMainSubmitVo = new ArchMainSubmitVo();
            archMainSubmitVo.setXData(xdata);
            archMainSubmitVo.setSeriesData(seriesData);
            archMainSubmitVo.setSubmitCountToday(submitCountToday);
            archMainSubmitVo.setAllPassCount(allPassCount);
            archMainSubmitVo.setAllNoPassCount(allNoPassCount);
            archMainSubmitVo.setIsAuditCount(isAuditCount);
            archMainSubmitVo.setAllPassCountToday(allPassCountToday);
            archMainSubmitVo.setAllPassCountYear(allPassCountYear);
            archMainSubmitVo.setAllSubmitCountYear(allSubmitCountYear);
            return Response.success(archMainSubmitVo);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询今日档案提交量失败：{}", e.getMessage());
            return Response.error("查询今日档案提交量失败");
        }
    }

    @Override
    public Response getPassRate() {
        try {
            PassRateVo passRateVo=new PassRateVo();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
            String time = simpleDateFormat.format(new Date());
            int allPassCountToday=digitalArchMainMapper.getPassCountToday(time,5);
            int allNoPassCountToday=digitalArchMainMapper.getPassCountToday(time,3);
            if(allPassCountToday+allNoPassCountToday==0){
                passRateVo.setPassRateToday("0.00");
            }else{
                DecimalFormat b = new DecimalFormat("0.00");
                float res=Float.parseFloat(String.valueOf(allPassCountToday))/(allNoPassCountToday+allPassCountToday)*100;
                passRateVo.setPassRateToday(b.format(res));
            }
            String year=time.substring(0,4);
            String month=time.substring(4,6);
            List<String> monthList=new ArrayList<>();
            List<Integer> submitCountList=new ArrayList<>();
            List<Integer> auditCountList=new ArrayList<>();
            List<String> passRateList=new ArrayList<>();
            if(Integer.parseInt(month)!=12){
                for (int i=Integer.parseInt(month)+1;i<=12;i++){
                    monthList.add(String.valueOf(Integer.parseInt(year)-1).substring(2,4)+"-"+i);
                    String param=(Integer.parseInt(year)-1)+String.format("%02d", i);
                    int submitCount=digitalArchMainMapper.getSubmitCountMonth(param);
                    submitCountList.add(submitCount);
                    int auditCount=digitalArchMainMapper.getAuditCountMonth(param);
                    auditCountList.add(auditCount);
                    int noPassCount=digitalArchMainMapper.getNoPassCountMonth(param);
                    if(auditCount+noPassCount==0){
                        passRateList.add("0.00");
                    }else{
                        DecimalFormat b = new DecimalFormat("0.00");
                        float res=Float.parseFloat(String.valueOf(auditCount))/(auditCount+noPassCount)*100;
                        passRateList.add(b.format(res));
                    }
                }
            }
            for (int i=1;i<=Integer.parseInt(month);i++){
                monthList.add(year.substring(2,4)+"-"+i);
                String param=year+String.format("%02d", i);
                int submitCount=digitalArchMainMapper.getSubmitCountMonth(param);
                submitCountList.add(submitCount);
                int auditCount=digitalArchMainMapper.getAuditCountMonth(param);
                auditCountList.add(auditCount);
                int noPassCount=digitalArchMainMapper.getNoPassCountMonth(param);
                if(auditCount+noPassCount==0){
                    passRateList.add("100.00");
                }else{
                    DecimalFormat b = new DecimalFormat("0.00");
                    float res=Float.parseFloat(String.valueOf(auditCount))/(auditCount+noPassCount)*100;
                    passRateList.add(b.format(res));
                }
            }
            passRateVo.setPassRate(passRateList);
            passRateVo.setMonth(monthList);
            passRateVo.setAuditCount(auditCountList);
            passRateVo.setSubmitCount(submitCountList);
            return Response.success(passRateVo);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询通过率失败：{}", e.getMessage());
            return Response.error("查询通过率失败");
        }
    }

}
